package cn.com.ddapp.ddm.ui.order;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.base.ActivityManager;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.fragment.base.order.OrderListFragment;
import cn.com.ddapp.ddm.ui.Refreshable;
import cn.com.ddapp.ddm.widget.viewpager.ViewPagerIndicator;

/**
 * Created by caopenghui on 2016-08-03.
 */
public class OrderListActiviy extends BaseActivity {

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.viewpger)
    ViewPager mViewPager;
    @BindView(R.id.viewpager_indicator)
    ViewPagerIndicator mIndicator;

    private List<String> mDatas;
    private FragmentPagerAdapter mAdapter;
    private List<Fragment> mTabContents = new ArrayList<Fragment>();
    private MyReceiver myReceiver;
    @Override
    public void initView() {

        mDatas = Arrays.asList(getString(R.string.order_all),
                getString(R.string.order_pay_waiting),
                getString(R.string.order_sending_waiting),
                getString(R.string.order_receive_waiting),getString(R.string.order_finish));

        for (int i=0; i<mDatas.size();i++) {
            OrderListFragment fragment = OrderListFragment
                    .newInstance(i);
            mTabContents.add(fragment);
        }

        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public int getCount() {
                return mTabContents.size();
            }

            @Override
            public Fragment getItem(int position) {
                return mTabContents.get(position);
            }
        };

        mViewPager.setOffscreenPageLimit(5);
        mIndicator = (ViewPagerIndicator)findViewById(R.id.viewpager_indicator);
        // 设置Tab上的标题
        mIndicator.setTabItemTitles(mDatas);
        mViewPager.setAdapter(mAdapter);
        // 设置关联的ViewPager
        mIndicator.setViewPager(mViewPager, 0);
        title.setText(BaseApplication.getIntstance().getResources().getString(R.string.my_order));
        back.setVisibility(View.VISIBLE);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ActivityManager.getInstance().finishActivity();
            }
        });
        myReceiver = new MyReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("updata");
        registerReceiver(myReceiver,intentFilter);
    }

    @Override
    public void initData() {


    }

    @Override
    public int getContentViewId() {
        return R.layout.order_list_activity;
    }

    @Override
    protected String getFragmentTag() {
        return OrderListActiviy.class.getSimpleName();
    }
    public class MyReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals("updata")){
                List<Fragment> fragments = getSupportFragmentManager().getFragments();
                for(Fragment fragment :fragments){
                    if(fragment instanceof Refreshable){
                        ((Refreshable) fragment).onRefresh();
                    }
                }
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myReceiver);
    }
}
